package com.bobi.tfca.ui.mainnavigation.user.apply

import androidx.fragment.app.Fragment
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.bobi.tfca.network.ApiResponse
import com.bobi.tfca.network.api.AuditAPI
import com.bobi.tfca.network.response.AuditResponse
import com.bobi.tfca.util.LogUtil
import com.bobi.tfca.viewmodel.PublicViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class MemApplyFragmentVM : ViewModel() {

    val audits by lazy {
        MutableLiveData(ArrayList<AuditResponse>())
    }

    fun findAllEntryApply(
        publicViewModel: PublicViewModel,
        memEntryApplyFragment: MemEntryApplyFragment
    ) {
        publicViewModel.apply {
            request(AuditAPI::class.java).findAllEntryApply().getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }

                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                val result = it.data?.data
                                if (result != null) {
                                    audits.value = result
                                    LogUtil.i("vv--" + result.toString())
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    fun findAllExitApply(
        publicViewModel: PublicViewModel,
        memExitApplyFragment: MemExitApplyFragment
    ) {
        publicViewModel.apply {
            request(AuditAPI::class.java).findAllExitApply().getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }

                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                val result = it.data?.data
                                if (result != null) {
                                    audits.value = result
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    fun passAudit(publicViewModel: PublicViewModel, auditId: Int, fragment: Fragment) {
        publicViewModel.apply {
            request(AuditAPI::class.java).passAudit(auditId).getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }

                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                // 再次查询
                                if (fragment is MemEntryApplyFragment) {
                                    findAllEntryApply(publicViewModel, fragment)
                                } else if (fragment is MemExitApplyFragment) {
                                    findAllExitApply(publicViewModel, fragment)
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    fun refuseAudit(publicViewModel: PublicViewModel, auditId: Int, fragment: Fragment) {
        publicViewModel.apply {
            request(AuditAPI::class.java).refuseAudit(auditId).getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }

                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                // 再次查询
                                if (fragment is MemEntryApplyFragment) {
                                    findAllEntryApply(publicViewModel, fragment)
                                } else if (fragment is MemExitApplyFragment) {
                                    findAllExitApply(publicViewModel, fragment)
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}